# -*- coding:utf-8 -*-
# author:zhou date:2019/5/10
import sys, pdb
import os, json
import django
from pprint import pprint

sys.path.append("..")
sys.path.append("../utils")
os.environ.update({"DJANGO_SETTINGS_MODULE": "server.settings"})
django.setup()
from django.test import Client

client = Client()
headers = {'Content-Type': 'application/xml'}

baseUrl = u"http://127.0.0.1:8000/api/software/"


def uploadSoft():
    choice = 'new'
    # choice = 'update'
    # new
    if choice == 'new':
        request = {
            'token': 'c91f508c-04c7-42ca-adc2-d85238208659@jw.pt',
            'softwareName': "google",
            'softwareDetails': "2020测试使用",
            'cost': "1221212",
            'version': "1.0",
            'storehouse': 1,
            'warZone': 1,  # ForeignKey
            'company': 1,  # ForeignKey
            'softwareType': 1,  # ForeignKey
            'logo': 1,  # address
            'cover': 1,  # address
            'verification': 1,  # address
            'isSeg': 1,
            'fileId': json.dumps([383, 384]),  # address
            'material': json.dumps([{
                'id': 1,
                'dataName': '资料手册',
            }, {
                'id': 2,
                'dataName': '资料手册',
            }]),
            'questionAnswer': json.dumps([{
                'id': 1,
                'dataName': '资料手册',
            }, {
                'id': 2,
                'dataName': '资料手册',
            }]),
        }
    # update
    else:
        request = {
            'token': '2744849f9117b8f10788',
            'softwareDetails': "2020测试使用",
            'softwareId': 7,
            'version': "1.0",
            'fileId': 1,  # address
            'cost': "1",
            'verification': 1,  # address
            'material': json.dumps([{
                'id': 1,
                'dataName': '资料手册',
                'updateDetails': '下载教学',
            }, {
                'id': 2,
                'dataName': '资料手册',
                'updateDetails': '下载教学',
            }]),
            'questionAnswer': json.dumps([{
                'id': 1,
                'dataName': '资料手册',
            }, {
                'id': 2,
                'dataName': '资料手册',
            }]),
        }
    res = client.post(baseUrl + 'upload/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    print(res.content)


def uploadfiles():
    request = {
        'token': '83764f261b574c03be2b',
        'files': 'E:\\softwareOnline2\\data\\software\\file\\20200419154958Glz9gFq9.exe',  # address
        'fileType': 'QAFile',
    }
    res = client.post(baseUrl + 'upfile/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest',
                      content_type="multipart/form-data")
    print(res.content)


def softDetail():
    request = {
        'token': 'c91f508c-04c7-42ca-adc2-d85238208659@jw.pt',
        'softwareVersionId': '24',
    }
    res = client.post(baseUrl + 'detail/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    print(res.content)


def softwarelist():
    # 管理人员
    choice = '管理人员'
    # 开发人员
    # choice = '开发人员'
    # 使用人员
    # choice = '使用人员'
    if choice == '管理人员':
        request = {
            # 'token': '61aa3da28253d3877c20',
            'versionId': 'zkjdfks',
            'softwareGroup': '0',
            'page': 1,
            'maxItem': 1,
        }
    elif choice == '开发人员':
        request = {
            'token': '83764f261b574c03be2b',
            'versionId': '',
            'softwareGroup': '',
            'page': '',
            'maxItem': '0',
        }
    else:
        request = {
            'token': '80dd9bc3de1a768f6d41',
            'versionId': '',
            'softwareGroup': '',
            'page': 1,
            'maxItem': 5,
        }
    res = client.post(baseUrl + 'list/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    print(res.content)


def softOperation():
    # 管理人员
    # choice = '管理人员'
    # 开发人员
    # choice = '开发人员'
    # 使用人员
    choice = '使用人员'
    if choice == '管理人员':
        request = {
            'token': '61aa3da28253d3877c20',
            'softwareVersionId': 47,
            'operation': 0,
            'verify': 1,
            'isApply': 1,
            'deliveryType': 1,
            'isView': json.dumps([2]),
            'isDownload': json.dumps([2]),
            'rejectReason': "拒绝拒绝拒绝拒绝拒绝拒绝拒绝拒绝拒绝拒绝"
        }
    elif choice == '开发人员':
        request = {
            'token': '83764f261b574c03be2b',
            'softwareVersionId': 28,
            'operation': 0,
            # 'operation': 1,
            'verify': '',
            'isApply': '',
            'deliveryType': '',
        }
    else:
        request = {
            'token': '80dd9bc3de1a768f6d41',
            'softwareVersionId': 29,
            'operation': 0,
            'verify': '',
            'isApply': '',
            'deliveryType': '',
            'orderId': 53
        }
    res = client.post(baseUrl + 'operate/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    print(res.content)


def softSearch():
    # 管理人员
    choice = '管理人员'
    # 开发人员
    # choice = '开发人员'
    # 使用人员
    # choice = '使用人员'
    if choice == '管理人员':
        request = {
            'token': '15f3911902953e46ce92',
            'softwareName': '',
            'uploadStartTime': '',
            'uploadEndTime': '',
            'storehouseId': '',
            'examineState': '',
            'softwareGroup': '0',
            'softwareTypeId': 2,
            'page': 1,
            'maxItem': 1,
        }
    elif choice == '开发人员':
        request = {
            'token': '83764f261b574c03be2b',
            'softwareName': '百',
            'uploadStartTime': '2020-2-20',
            'uploadEndTime': '2020-3-30',
            'storehouseId': '1',
            'softwareTypeId': 2,
            'examineState': '',
            'softwareGroup': '0',
            'page': 1,
            'maxItem': 1,
        }
    else:
        request = {
            'token': '80dd9bc3de1a768f6d41',
            'softwareName': '',
            'uploadStartTime': '2020-3-20',
            'uploadEndTime': '2020-3-30',
            'storehouseId': '',
            'softwareTypeId': 2,
            'examineState': '',
            'softwareGroup': '0',
            'page': 1,
            'maxItem': 1,
        }
    res = client.post(baseUrl + 'research/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    print(res.content)


def groupList():
    request = {
        'token': '61aa3da28253d3877c20',
        'page': 1,
        'maxItem': 5,
        'softwareGroup': '0',
    }
    res = client.post(baseUrl + 'groupList/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    print(res.content)


def groupSearch():
    request = {
        'token': '80dd9bc3de1a768f6d41',
        'page': 1,
        'maxItem': 5,
        'groupName': '',
        'category': '1',
        'softwareGroup': '0',
    }
    res = client.post(baseUrl + 'groupScreen/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    print(res.content)


def groupDetail():
    request = {
        'token': '80dd9bc3de1a768f6d41',
        'groupId': 2,
    }
    res = client.post(baseUrl + 'groupDetail/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    print(res.content)


def groupOperate():
    request = {
        'token': '61aa3da28253d3877c20',
        'groupId': 2,
        'operate': '3',
    }
    res = client.post(baseUrl + 'groupOperate/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    print(res.content)


def groupServiceOperate():
    request = {
        'token': '4f3f9bc4-5b43-441a-8b0f-9870f30c6b69@jw.pt',
        'groupName': '编组1号',
        'groupDetail': '编组1号概述',
        'category': '1',
        'planName': '80dd9bc3de1a768f6d4180dd9bc3de1a768f6d4180dd9bc3de1a768f6d41',
        'planContent': '80dd9bc3de1a768f6d4180dd9bc3de1a768f6d4180dd9bc3de1a768f6d41',
        'selectSoftware': json.dumps([
            {
                'softwareId': 1,
                'softwareVersion':
                    [
                        {'id': 1, 'ratio': 0.5},
                    ]
            },
            {
                'softwareId': 2,
                'softwareVersion':
                    [
                        {'id': 2, 'ratio': 0.5},
                    ]
            },
            {
                'softwareId': 3,
                'softwareVersion':
                    [
                        {'id': 3, 'ratio': 0.5},
                    ]
            },
        ]),
    }
    res = client.post(baseUrl + 'groupServiceOperate/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    print(str(res.content, 'utf-8'))


def groupDownLoad():
    request = {
        'token': '4f3f9bc4-5b43-441a-8b0f-9870f30c6b69@jw.pt',
        'groupId': 1,
    }
    res = client.post(baseUrl + 'groupDownload/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    print(res.content)


def detailGet():
    request = {
        'token': '15f3911902953e46ce92',
        'softwareVersionId': 1,
    }
    res = client.post(baseUrl + 'softUpdate/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    print(res.content)


def qaGet():
    request = {
        'token': '2744849f9117b8f10788',
    }
    res = client.post(baseUrl + 'requestQA/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    print(res.content)


def getDuanInfoServer():
    # request = {
    #     'token': '2744849f9117b8f10788',
    # }
    res = client.get(baseUrl + 'duanInfoServer/', HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    pprint(json.loads(res.content))


def pushDuanToOther():
    request = {
        'token': '8dbd6569-9c46-49be-bd67-d106f36c92ef@jw.pt',
        'duanId': 3
    }
    res = client.post(baseUrl + 'pushDuanFile/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    pprint(json.loads(res.content))


def fastDFS():
    from fdfs_client.client import Fdfs_client, get_tracker_conf
    from conf.sysConfig import config
    FastDFS_config = config['FastDFS_config']
    tracker_path = get_tracker_conf(FastDFS_config)
    client = Fdfs_client(tracker_path)
    import time
    start = time.time()
    # ret = client.upload_by_filename('E:\\1209\\ZHMGSN.zip')
    ret = client.upload_by_filename('C:\\Users\\admin\\Downloads\\chrome.jpg')
    end = time.time()
    # ret = client.upload_by_filename('/home/ckgit/server/requirements.txt')
    print(ret)
    print("花费：" + str(end - start))
    if ret.get("Status") != "Upload successed.":
        pass
    fileName = ret.get('Remote file_id').decode()
    size = ret.get('Uploaded size')
    print(fileName)
    print(size)


def containerList():
    request = {
        'token': '8dbd6569-9c46-49be-bd67-d106f36c92ef@jw.pt',
        'page': 6,
        'maxItem': 10
    }
    res = client.post(baseUrl + 'containerList/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    pprint(json.loads(res.content))


def deleteFile():
    request = {
        'token': '3e55b8ab838fa5f93668',
        'id': 23,
        'versionId': 17,
        'option': "0"
    }
    res = client.post(baseUrl + 'deleteFile/', request, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    pprint(json.loads(res.content))


if __name__ == '__main__':
    # uploadSoft()
    # uploadfiles()
    # softDetail()
    # softwarelist()
    # softOperation()
    # softSearch()
    # groupList()
    # groupSearch()
    # groupDetail()
    # groupOperate()
    # groupServiceOperate()
    # groupDownLoad()
    # detailGet()
    # qaGet()
    # getDuanInfoServer()
    # pushDuanToOther()
    # containerList()
    # fastDFS()
    deleteFile()
